<template>
  <div class="bilibili-ranking-card">
    <div class="card-header">
      <h3 class="card-title">
        <el-icon><Trophy /></el-icon>
        热门排行榜
      </h3>
      <el-button type="text" size="small" @click="handleViewAll">
        查看更多
        <el-icon><ArrowRight /></el-icon>
      </el-button>
    </div>

    <div class="ranking-list">
      <div
          v-for="item in displayedRankings"
          :key="item.rank"
          class="ranking-item"
          :class="getRankClass(item.rank)"
          @click="handleItemClick(item)"
      >
        <div class="rank-number">{{ item.rank }}</div>

        <div class="item-cover">
          <img :src="item.cover" :alt="item.title" />
        </div>

        <div class="item-info">
          <h4 class="item-title" :title="item.title">{{ item.title }}</h4>
          <div class="up-name">{{ item.upName }}</div>
          <div class="item-stats">
            <span class="play-count">
              <el-icon><View /></el-icon>
              {{ formatCount(item.playCount) }}
            </span>
          </div>
        </div>

        <div class="rank-change" :class="getChangeClass(item.change)">
          <el-icon v-if="item.change > 0"><Top /></el-icon>
          <el-icon v-else-if="item.change < 0"><Bottom /></el-icon>
          <span v-else>-</span>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div v-if="!rankings.length" class="empty-ranking">
      <el-empty description="暂无排行数据" :image-size="60" />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { Trophy, View, ArrowRight, Top, Bottom } from '@element-plus/icons-vue'

const props = defineProps({
  rankings: {
    type: Array,
    default: () => []
  },
  title: {
    type: String,
    default: '热门排行榜'
  },
  maxDisplay: {
    type: Number,
    default: 5
  },
  type: {
    type: String,
    default: 'video' // video, live, up
  }
})

const emit = defineEmits(['item-click', 'view-all'])

const router = useRouter()

// 计算属性
const displayedRankings = computed(() => {
  return props.rankings.slice(0, props.maxDisplay)
})

// 处理方法
const handleItemClick = (item) => {
  emit('item-click', item)
  if (props.type === 'video') {
    router.push(`/video/${item.id}`)
  }
}

const handleViewAll = () => {
  emit('view-all')
  router.push('/ranking')
}

// 工具函数
const formatCount = (count) => {
  if (count >= 10000) {
    return (count / 10000).toFixed(1) + '万'
  }
  return count.toString()
}

const getRankClass = (rank) => {
  if (rank === 1) return 'rank-first'
  if (rank === 2) return 'rank-second'
  if (rank === 3) return 'rank-third'
  return 'rank-other'
}

const getChangeClass = (change) => {
  if (change > 0) return 'change-up'
  if (change < 0) return 'change-down'
  return 'change-same'
}
</script>

<style scoped>
.bilibili-ranking-card {
  background: white;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f0f0;
}

.card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #18191c;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ranking-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.ranking-item:hover {
  background: #f5f7fa;
}

.rank-number {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}

.rank-first .rank-number {
  background: linear-gradient(45deg, #ffd23f, #ffb347);
  color: white;
}

.rank-second .rank-number {
  background: linear-gradient(45deg, #c1c1c1, #a8a8a8);
  color: white;
}

.rank-third .rank-number {
  background: linear-gradient(45deg, #d4a574, #c19a6b);
  color: white;
}

.rank-other .rank-number {
  background: #f0f0f0;
  color: #666;
}

.item-cover {
  width: 40px;
  height: 25px;
  border-radius: 3px;
  overflow: hidden;
  flex-shrink: 0;
}

.item-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-info {
  flex: 1;
  min-width: 0;
}

.item-title {
  margin: 0 0 2px 0;
  font-size: 12px;
  font-weight: 500;
  color: #18191c;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.up-name {
  font-size: 10px;
  color: #999;
  margin-bottom: 2px;
}

.item-stats {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  color: #ccc;
}

.play-count {
  display: flex;
  align-items: center;
  gap: 2px;
}

.rank-change {
  display: flex;
  align-items: center;
  gap: 2px;
  font-size: 10px;
  font-weight: 500;
  flex-shrink: 0;
}

.change-up {
  color: #ff6b6b;
}

.change-down {
  color: #4ecdc4;
}

.change-same {
  color: #999;
}

.empty-ranking {
  padding: 20px 0;
  text-align: center;
}
</style>